/* Autostop
 *
 * Copyright (c) 2006 - 2007 Maciej Gajewski, Grzegorz Latarowski
 * http://code.google.com/p/autostop-automaty-drogowe
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version. NO WARRANTY OF ANY KIND is expressed or implied.
 */

#include <assert.h>			// uses assert()

#include "drawing.h"

// min/max defnitions. note - these aree gcc-only
#define min(i,j)	((i) < (j) ? (i) : (j))
#define max(i,j)	((i) > (j) ? (i) : (j))

// ============================================================
/// Draws inversed rectangle on image
void draw_rectangle( pimage_t p_image, int minx, int miny, int maxx, int maxy )
{
	int x, y;
	
	assert( p_image );
	assert( p_image->width > minx );
	assert( p_image->width > maxx );
	assert( p_image->height > miny );
	assert( p_image->height > maxy );
	assert( minx <= maxx );
	assert( miny <= maxy );
	// TODO and many more... does it have a point?
	
	// draw horizontal lines
	for( x = minx; x <= maxx; x++)
	{
		// upper line
		if ( image_pixel( p_image, x, miny ) > 128 )
		{
			image_pixel( p_image, x, miny ) = 0;
		}
		else
		{
			image_pixel( p_image, x, miny ) = 255;
		}
		
		// lower line
		if ( image_pixel( p_image, x, maxy ) > 128 )
		{
			image_pixel( p_image, x, maxy ) = 0;
		}
		else
		{
			image_pixel( p_image, x, maxy ) = 255;
		}
	}
	
	// draw vertical
	for( y = miny; y <= maxy; y++)
	{
		// left line
		if ( image_pixel( p_image, minx, y ) > 128 )
		{
			image_pixel( p_image, minx, y ) = 0;
		}
		else
		{
			image_pixel( p_image, minx, y ) = 255;
		}
		
		// right line
		if ( image_pixel( p_image, maxx, y ) > 128 )
		{
			image_pixel( p_image, maxx, y ) = 0;
		}
		else
		{
			image_pixel( p_image, maxx, y ) = 255;
		}
	}
}

// ============================================================
/// Draws inversed cross on image
void draw_cross( pimage_t p_image, int x, int y, int size )
{
	int i;
	
	assert( p_image );
	
	// horizontal line
	for( i = max( 0, x - size ) ; i <= min ( x + size, p_image->width ) ; i++ )
	{
		if ( image_pixel( p_image, i, y ) > 128 )
		{
			image_pixel( p_image, i, y ) = 0;
		}
		else
		{
			image_pixel( p_image, i, y ) = 255;
		}
	}
	// vertical line
	for( i = max( 0, y - size ) ; i <= min ( y + size, p_image->height ) ; i++ )
	{
	
		if ( image_pixel( p_image, x, i ) > 128 )
		{
			image_pixel( p_image, x, i ) = 0;
		}
		else
		{
			image_pixel( p_image, x, i ) = 255;
		}
	}
}


// eof

